<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>


        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a id="deleteButton" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="/lib/layui/layui.js" charset="utf-8"></script>
<script>

    layui.use(['jquery', 'form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        $.ajax({
            url: "http://192.168.100.222:8082/user/checking.do",
            type: "post",
            dataType: "json",
            success: function (rec) {
                if (rec.code !== 0) {
                    layer.msg("非管理员无权访问查询页面,请联系管理员", {icon: 5, time: 1500}, function () {
                        window.location.href = "/html/welcome.html?t=1";
                    })
                } else {
                    init();
                }
            }
        });

        function init() {
            let userTable = table.render({
                elem: '#currentTableId',
                url: 'http://192.168.100.222:8082/user/findAll.do?level=2',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    {field: 'id', title: 'ID'},
                    {field: 'username', title: '用户名'},
                    {field: 'roleName', title: '会员类型'},
                    {field: 'referCode', title: '推荐人姓名'},
                    {
                        field: 'createTime',
                        title: '创建时间',
                        templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                    },
                    {
                        field: 'lastUpdateTime',
                        title: '最后修改时间',
                        templet: "<div>{{layui.util.toDateString(d.lastUpdateTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                    },
                    {
                        fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150
                    }
                ]],
                limits: [5, 10, 15, 20, 25, 50, 100],
                limit: 5,
                page: true,
                skin: 'line'
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var result = JSON.stringify(data.field);
                let parse = JSON.parse(result);
                console.log("4" + parse.username);
                //执行搜索重载
                table.reload('currentTableId', {
                    where: {
                        username: parse.username
                    }
                });
                return false;
            });
            /**
             * toolbar监听事件
             */
            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                console.log(obj);
                console.log(data);
                if (obj.event === 'edit') {
                    var index = layer.open({
                        title: '编辑用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/html/user/register_edit.html',
                        success: function (layero, index) {
                            var iframe = window['layui-layer-iframe' + index];//拿到iframe元素
                            iframe.child(data)//向此iframe层方法 传递参数
                        },
                        end: function () {
                            userTable.reload();
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    if (data.roleName !== "注册会员") {
                        layer.msg("非注册会员无法被删除", {icon: 5});
                        return false;
                    }
                    layer.confirm('确定删除?', function (index) {
                        $.ajax({
                            url: "http://192.168.100.222:8082/user/deleteUser.do",
                            type: "get",
                            data: {id: data.id},
                            dataType: "json",
                            success: function (rec) {
                                if (rec.code === 0) {
                                    obj.del();
                                    layer.msg("删除成功", {icon: 6})
                                } else {
                                    layer.msg("删除失败", {icon: 5});
                                }
                            }
                        })
                    });
                }
            });
        }

    });
</script>

</body>
</html>